<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .scroll {
            width: 600px;
            height: 8px;
            background: #f0f0f0;
            margin: 100px;
            position: relative;
            border: 2px solid #c0c0c0;
            border-radius: 8px;
        }

        .scroll .bar {
            width: 15px;
            height: 15px;
            border-radius: 100%;
            background: #b0cae9;
            border: 3px solid #75a2da;
            position: absolute;
            z-index: 3;
            top: -7px;
            left: -3px;
            cursor: pointer;
        }

        .scroll .mask {
            /* width: 0px; */
            height: 100%;
            border-radius: 8px;
            background: #9ebefa;
            position: absolute;
            top: 0px;
            left: 0;
            z-index: 2;
        }

        .num {
            position: absolute;
            top: -25px;
            left: -4px;
            display: none;
            color: #333;
        }
    </style>
</head>

<body>
    <div class="scroll" id="scrollBox">
        <div class="bar" id="bar"></div>
        <div class="mask" id="mask"></div>
        <div id="num" class="num"></div>
    </div>


    <script>
        var scrollbox = document.getElementById("scrollBox");
        var bar = scrollbox.firstElementChild;
        var mask = scrollbox.children[1];
        var num = document.getElementById("num");

        bar.onmousedown = function () {
            num.style.display = "block";

            document.onmousemove = function (event) {

                var x = event.pageX - scrollbox.offsetLeft;
                if (x > scrollbox.offsetWidth - bar.offsetWidth) {
                    x = scrollbox.offsetWidth - bar.offsetWidth;
                } else if (x < 0) {
                    x = -3;
                }
                mask.style.width = x + 3 + "px";
                bar.style.left = x + "px";
                num.style.left = x + "px";

                num.innerText = parseInt(x / (scrollbox.offsetWidth - bar.offsetWidth) * 100) + "%"


                // 防止拖动
                window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
            }
        }

        document.onmouseup = function () {
            num.style.display = "none";
            document.onmousemove = null;
        }
    </script>
</body>

</html>